package com.chery.media.ui.main.app

import android.os.Bundle
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.LogUtils
import com.chery.media.R
import com.chery.media.databinding.FragmentMainAppListBinding
import com.chery.media.library.base.BaseVMFragment
import com.chery.media.library.base.NoViewModel
import com.chery.media.library.extend.dp
import com.chery.media.library.extend.setNbOnItemClickListener
import com.chery.media.library.extend.toJson
import com.chery.media.library.widget.recyclerview.SpacesItemDecoration
import com.chery.media.network.model.AppInfoBean
import com.chery.media.ui.main.AppAdapter

class AppListFragment(val appInstall: ArrayList<AppInfoBean> = arrayListOf()): BaseVMFragment<NoViewModel, FragmentMainAppListBinding>() {

    private val appAdapter by lazy {
        AppAdapter().apply {
            setNbOnItemClickListener { adapter, view, position ->
                val item = getItem(position)
                if ((item.packageName?:"").isNullOrEmpty().not()) {
                    val packageManager = requireActivity().packageManager
                    val intent = packageManager.getLaunchIntentForPackage(item.packageName?:"")
                    startActivity(intent)
                }
            }
        }
    }

    override val layoutId: Int
        get() = R.layout.fragment_main_app_list


    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        mBinding.appList.addItemDecoration(SpacesItemDecoration(6, 5.dp, false))
        mBinding.appList.adapter = appAdapter
        appAdapter.setList(appInstall)
    }

    companion object {
        fun newInstance(installApp: ArrayList<AppInfoBean> = arrayListOf()) = AppListFragment(installApp)
    }

}